﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DisplayBoardInfo.Server.Model;

namespace DisplayBoardInfo.Server.Entities
{
    public class User : Entity
    {
        private Model.User user;

        /// <summary>
        /// Creates new, empty entity.
        /// </summary>
        public User()
            : base()
        {
            user = new Model.User();
            db.Users.InsertOnSubmit(user);
        }

        /// <summary>
        /// Create specific instance of entity.
        /// </summary>
        /// <remarks>
        /// If user with that username already exist, It will be load. In otherwise will be create a new one.
        /// </remarks>
        public User(string username)
            : base()
        {
            if (Users.IsUserExists(username))
            {
                user = db.Users.Single(i => i.Username == username);
            }
            else
            {
                user = new Model.User();
                user.Username = username;
            }
        }

        /// <summary>
        /// Refresh all data in entity from database.
        /// </summary>
        public override void Refresh()
        {
            db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, user);
        }

        /// <summary>
        /// Saves changes in database.
        /// </summary>
        public override void Save()
        {
            db.SubmitChanges();
        }

        /// <summary>
        /// Gets or sets username
        /// </summary>
        public string Username
        {
            get 
            {
                return user.Username;
            }

            set
            {
                if (value.Length > 30 || value.Length < 3)
                {
                    throw new ArgumentOutOfRangeException("Nazwa użytkownika nie musi składać się z co najmniej 3 znaków i być nie dłuższa niż 30.");
                }
                else user.Username = value;
            }
        }

        /// <summary>
        /// Gets password in SHA256 hash and sets in plain text
        /// </summary>
        public string Password
        {
            get
            {
                return user.Password;
            }

            set
            {
                if (value.Length < 5)
                {
                    throw new ArgumentOutOfRangeException("Hasło musi mieć co najmniej 5 znaków.");
                }
                else user.Password = Helpers.Data.GetSHAHash(value);
            }
        }

        /// <summary>
        /// Gets role of user.
        /// </summary>
        public Role Role
        {
            get 
            {
                return new Role(user.Role1);
            }
        }

        /// <summary>
        /// Gets or sets name of role which user belongs.
        /// </summary>
        public string RoleName
        {
            get
            {
                return user.Role;
            }

            set
            { 
                if(Roles.IsRoleExists(value))
                {
                    user.Role = value;
                }
                else throw new ArgumentOutOfRangeException("Rola nie istnieje");
            }
        }

        /// <summary>
        /// Gets or sets user name.
        /// </summary>
        public string Name
        {
            get 
            {
                return user.Name;
            }

            set
            {
                user.Name = value;
            }
        }

        /// <summary>
        /// Gets or sets user last name
        /// </summary>
        public string LastName
        {
            get
            {
                return user.LastName;
            }

            set
            {
                user.LastName = value;
            }
        }

        /// <summary>
        /// Gets or sets user position
        /// </summary>
        public string Position
        {
            get
            {
                return user.Position;
            }

            set
            {
                user.Position = value;
            }
        }

        /// <summary>
        /// Gets or sets photo of user.
        /// </summary>
        public System.Drawing.Image Photo
        {
            get
            {
                return Helpers.Data.ByteArrayToImage(user.Photo.ToArray());
            }

            set
            {
                user.Photo = Helpers.Data.ImageToByteArray(value, System.Drawing.Imaging.ImageFormat.Bmp);
            }
        }

        /// <summary>
        /// Gets or sets photo of user.
        /// </summary>
        public byte[] PhotoInBytes
        {
            get
            {
                return user.Photo.ToArray();
            }
        }

        public override string ToString()
        {
            if (!String.IsNullOrEmpty(Name) && !String.IsNullOrEmpty(LastName)) return Username + " (" + Name + " " + LastName + ")";
            else return Username;
        }
    }
}
